package com.huiqiao.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huiqiao.sys.constant.SysConstant;
import com.huiqiao.sys.domain.*;
import com.huiqiao.sys.mapper.MenuMapper;
import com.huiqiao.sys.mapper.RoleMapper;
import com.huiqiao.sys.mapper.RoleMenuMapper;
import com.huiqiao.sys.service.RoleService;
import com.huiqiao.sys.utils.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author: yanliwei@foolish
 * @CreateTime: 2024-12-22  16:50
 */

@Service
@Transactional //事务控制
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public DataGridView initRoleMenuTreeJson(Integer roleid) {
        //查询所有可用的菜单
        List<Menu> allMenus = menuMapper.select(new Menu().setAvailable(SysConstant.AVAILABLE_TRUE));
        //查询当前角色拥有的菜单
//        menuMapper.queryMenuByRoleId(SysConstant.AVAILABLE_TRUE,roleid);
        List<RoleMenu> roleMenus = roleMenuMapper.select(new RoleMenu().setRid(roleid));

        List<Menu> roleMenu = roleMenus.stream().map(RoleMenu::getMid)
                .map(mid -> menuMapper.selectByPrimaryKey(mid))
//                .map(menuMapper::selectByPrimaryKey)
                //排除空指针的办法
                .filter(Objects::nonNull)
                //去重
                .distinct()
                .collect(Collectors.toList());
        List<TreeNode> data = new ArrayList<>();
        for (Menu allMenu : allMenus) {
            String checkArr = SysConstant.CODE_ZERO + "";
            for (Menu rolemenu : roleMenu) {
                if (allMenu.getId() == rolemenu.getId()){
                    checkArr = SysConstant.CODE_ONE + "";
                    break;
                }
            }
            Integer id = allMenu.getId();
            Integer pid = allMenu.getPid();
            String title = allMenu.getTitle();
            Boolean spread = allMenu.getSpread() == SysConstant.SPREAD_FALSE ? false : true;
            data.add(new TreeNode(id, pid, title, spread, checkArr));

        }

        return new DataGridView(data);

    }

    @Override
    public DataGridView loadAllRole(RoleVo roleVo) {
        Page<Object> page = PageHelper.startPage(roleVo.getPage(), roleVo.getLimit());
        Weekend<Role> weekend = Weekend.of(Role.class);
        WeekendCriteria<Role, Object> roleObjectWeekendCriteria = weekend.weekendCriteria();
        //请输入角色名称
        if (StrUtil.isNotBlank(roleVo.getRolename())){
            roleObjectWeekendCriteria.andLike(Role::getRolename, "%" + roleVo.getRolename() + "%");
        }
        //请输入角色备注
        if (StrUtil.isNotBlank(roleVo.getRoledesc())){
            roleObjectWeekendCriteria.andLike(Role::getRoledesc, "%" + roleVo.getRoledesc() + "%");
        }
        //是否可用 available
        if (Objects.nonNull(roleVo.getAvailable())){
            roleObjectWeekendCriteria.andEqualTo(Role::getAvailable, roleVo.getAvailable());
        }
        List<Role> roles = roleMapper.selectByExample(weekend);
        return new DataGridView(page.getTotal(), roles);

    }

    @Override
    public void updateRole(RoleVo roleVo) {
        roleMapper.updateByPrimaryKeySelective(roleVo);
    }

    @Override
    public void deleteRole(RoleVo roleVo) {
        roleMapper.deleteByPrimaryKey(roleVo.getRoleid());
    }

    @Override
    public void addRole(RoleVo roleVo) {
        roleMapper.insertSelective(roleVo);
    }

    @Override
    public void deleteBatchRole(Integer[] ids) {
        Arrays.stream(ids).forEach(id -> roleMapper.deleteByPrimaryKey(id));
    }

    //保存角色和菜单的关系
    @Override
    public void saveRoleMenu(RoleVo roleVo) {
        Integer roleid = roleVo.getRoleid();
        Integer[] mids = roleVo.getIds();
        //根据id删除sys_role_menu里面的数据
        this.roleMenuMapper.delete(new RoleMenu().setRid(roleid));
        //保存角色和菜单的关系
        for (Integer mid : mids) {
            this.roleMenuMapper.insertSelective(new RoleMenu().setRid(roleid).setMid(mid));
        }
    }


}
